﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Taxonomy;
using System.Web.UI.WebControls;

namespace LB.GlobalSuiteLinks.Layouts.LB.GlobalSuiteLinks
{
    public partial class SuiteBarConfig : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Check for postback.
            if (!Page.IsPostBack)
            {
                TaxonomySession session = new TaxonomySession(SPContext.Current.Site);
                //Check that a term session exists.
                if (session != null)
                {
                    //Conntect to the default term store.
                    TermStoreCollection termstores = session.TermStores;
                    if (termstores.Count > 0)
                    {
                        TermStore defaultTermStore = termstores[0];

                        //Iterate the term store groups.
                        foreach (var item in defaultTermStore.Groups)
                        {
                            //Iterate the term sets for each group.
                            foreach (var set in item.TermSets)
                            {
                                try
                                {
                                    //Try to create a new list item if the term set is a navigation term set.
                                    if (set.CustomProperties["_Sys_Nav_IsNavigationTermSet"] != null)
                                    {
                                        TermSetList.Items.Add(new ListItem(String.Format("{0} [{1}]", set.Name, set.Group.Name), String.Format("{0},{1}", set.Id.ToString(), set.Group.Id.ToString())));
                                    }
                                }
                                catch { }
                            }
                        }
                    }

                    using (SPSite site = new SPSite(SPContext.Current.Site.Url))
                    {
                        SPWeb rootWeb = site.RootWeb;
                        if (termstores.Count > 0)
                        {
                            if (TermSetList.Items.Count != 0)
                            {
                                //Check that property bag entries exist for the root web
                                if (rootWeb.Properties != null && rootWeb.Properties.Count > 0)
                                {
                                    //Get the value of our property.
                                    String propertyValue = rootWeb.Properties["SuiteBarTermStoreId"].ToString();
                                    if (propertyValue != "1")
                                    {
                                        TermSetList.SelectedValue = propertyValue;

                                        //Check that an item was set, if not remove the property value as the term set may have been deleted.
                                        if (TermSetList.SelectedValue == null || TermSetList.SelectedValue == string.Empty)
                                        {
                                            rootWeb.Properties["SuiteBarTermStoreId"] = "1";
                                        }
                                    }
                                }
                            }
                            else
                            {
                                rootWeb.Properties["SuiteBarTermStoreId"] = "1";
                            }
                            rootWeb.Update();
                        }

                        try
                        {
                            ReplaceLinks.Checked = Convert.ToBoolean(rootWeb.Properties["SuiteBarOveride"]);
                        }
                        catch {
                            ReplaceLinks.Checked = false;
                        }

                        rootWeb.Dispose();
                    }
                }
            }
            //Check for associated managed metadata service application,disable controls and feedback if one is not.
            //Iterate the store to provide a list of navigation endabled termsets.
            //On Click, store the termset id and its parent termgroup id in properties.
            //Return back to site admin.
        }

        protected void topOKButton_Click(object sender, EventArgs e)
        {
            //does the property already exist?
            using (SPSite site = new SPSite(SPContext.Current.Site.Url))
            {
                SPWeb rootWeb = site.RootWeb;
                rootWeb.Properties["SuiteBarTermStoreId"] = TermSetList.SelectedValue;
                rootWeb.Properties["SuiteBarOveride"] = ReplaceLinks.Checked.ToString();
                rootWeb.Properties.Update();
                rootWeb.Dispose();
                Page.Response.Redirect("/_layouts/15/settings.aspx", true);
            }
        }

        protected void topCancelButton_Click(object sender, EventArgs e)
        {
            Page.Response.Redirect("/_layouts/15/settings.aspx", true);
        }
    }
}
